package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ExamPoints;
import com.jyy.prefabricated.service.ExamPointsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 考核点
 *
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ExamPointsController {

    @Autowired
    private ExamPointsService examPointsService;

    /**
     * 新增考核点
     *
     * @param examPoints
     * @return
     */
    @PostMapping("/exam-points")
    public AjaxResult insert(@Valid @RequestBody ExamPoints examPoints) {
        log.info("Rest request to insert examPoints:{}", examPoints);
        examPoints.setInventory(examPoints.getMachineCount());
        int id = examPointsService.insert(examPoints);
        ExamPoints result = examPointsService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新考核点
     *
     * @param examPoints
     * @return
     */
    @PutMapping("/exam-points")
    public AjaxResult update(@Valid @RequestBody ExamPoints examPoints) {
        log.info("Rest request to update examPoints:{}", examPoints);
        ExamPoints epResult = examPointsService.selectById(examPoints.getId());
        //判断机器数量是否已修改，如果修改，库存相应改变
        if (examPoints.getMachineCount() != null) {
            if (examPoints.getMachineCount() > epResult.getMachineCount()) {

                examPoints.setInventory(epResult.getInventory() + epResult.getMachineCount() - epResult.getMachineCount());
            } else {
                examPoints.setInventory(epResult.getInventory() + epResult.getMachineCount() - epResult.getMachineCount());
            }
        }
        examPointsService.update(examPoints);
        ExamPoints result = examPointsService.selectById(examPoints.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询考核点详情
     *
     * @param id
     * @return
     */
    @GetMapping("/exam-points/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find examPoints by id:{}", id);
        ExamPoints result = examPointsService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除考核点
     *
     * @param id
     * @return
     */
    @DeleteMapping("/exam-points/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete examPoints by id:{}", id);
        int row = examPointsService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询考核点列表
     *
     * @param name     名称
     * @param status   状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/exam-points")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status, Pageable pageable) {
        log.info("Rest request to find examPoints by params name:{},status:{},categoryId:{},lecturer:{}", name, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ExamPoints> examPoints = examPointsService.selectByParams(name, status);
        PageInfo<ExamPoints> result = new PageInfo<>(examPoints);
        return AjaxResult.success(result);
    }


    /**
     * 查询所有考核点包含的地区信息列表
     *
     * @return
     */
    @GetMapping("/exam-points-area")
    public AjaxResult selectByParams() {
        log.info("Rest request to find examPoints area");
        List<Map<String, Object>> result = examPointsService.findAreaList();
        return AjaxResult.success(result);
    }

    /**
     * 查询考核点列表
     *
     * @param name   名称
     * @param status 状态
     * @return
     */
    @GetMapping("/exam-points-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status) {
        log.info("Rest request to find examPoints by params name:{},status:{},categoryId:{},lecturer:{}", name, status);
        List<ExamPoints> result = examPointsService.selectByParams(name, status);
        return AjaxResult.success(result);
    }

    /**
     * 查询考核点列表
     *
     * @param province 省
     * @param city     市区
     * @return
     */
    @GetMapping("/exam-points-query")
    public AjaxResult selectByProvinceAndCity(@RequestParam(required = false, defaultValue = "") String province, @RequestParam(required = false, defaultValue = "") String city) {

        List<ExamPoints> result = examPointsService.selectByProvinceAndCity(province, city);
        return AjaxResult.success(result);
    }

    /**
     * 查询所有考核点列表  分组
     * @return
     */
    @GetMapping("/exam-points-group")
    public AjaxResult selectALL(@RequestParam(required = false, defaultValue = "") String province, @RequestParam(required = false, defaultValue = "") String city) {

        List<ExamPoints> result = examPointsService.selectAll(province, city);
        return AjaxResult.success(result);
    }
}
